import View from "../../../GameFramework/MVC/View";
import { EPokerStatus, ESuit } from "../../ConfigESuit";
import { Poker } from "../../GameScene/GameDB";
import GameEvent from "../../GameScene/GameEvents";
import GameView from "../../GameScene/GameView/GameView";

const POINT_MAP = {
    "1": "A",
    "2": "2",
    "3": "3",
    "4": "4",
    "5": "5",
    "6": "6",
    "7": "7",
    "8": "8",
    "9": "9",
    "10": "10",
    "11": "J",
    "12": "Q",
    "13": "K",
};
const { ccclass, property } = cc._decorator;
@ccclass
export default class UIPoker extends View {

    @property(cc.Sprite) BigsuitSprite: cc.Sprite = null;
    @property(cc.Sprite) bgSprite: cc.Sprite = null;

    @property(cc.Sprite) SmallsuitSprite: cc.Sprite = null;

    @property(cc.Label) pointLable: cc.Label = null;

    @property([cc.SpriteFrame]) bigSuits: cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame]) smallSuits: cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame]) texFaces: cc.SpriteFrame[] = [];
    @property(cc.SpriteFrame) texFrontBG: cc.SpriteFrame = null;
    @property(cc.SpriteFrame) texBackBG: cc.SpriteFrame = null;

    private redTextColor: cc.Color = cc.color(183, 24, 40);
    private blackTextColor: cc.Color = cc.Color.BLACK;
    private _poker: Poker=null;
    private view: GameView=null;
    public get poker():Poker{return this._poker}
    start(){
        //注册触摸事件
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this)
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this)
    }
    onDestroy(): void {
        //销毁触摸事件
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }
    //事件处理函数
    onTouchStart(event) {
        let x = this.node.convertToNodeSpaceAR(event).x
        console.log("onTouchStart x:", x);

    }
    onTouchMove(event) {
        let x = this.node.convertToNodeSpaceAR(event).x
        console.log("onTouchMove x:", x);
    }
    onTouchEnd(event) {
        // let x = this.node.convertToNodeSpaceAR(event)
        // this.view.emit(GameEvent.CLICK_POKER,this.poker)
        this.view.OnClickUIPoker(this)
    }
    onTouchCancel(event) {
        let x = this.node.convertToNodeSpaceAR(event).x
        console.log("onTouchCancel x:", this);
    }

    public Init(poker: Poker,view:GameView) {
        poker.Bind(this);
        this._poker = poker
        this.view = view
        
        // 点数
        this.pointLable.string = POINT_MAP[poker.point];
        //颜色
        this.pointLable.node.color =
            poker.suit == ESuit.HEITAO || poker.suit == ESuit.MEIHUA ? this.blackTextColor : this.redTextColor;
        //花色
        if (poker.point >= 11) {
            this.BigsuitSprite.spriteFrame = this.texFaces[poker.point - 11];
        } else {
            this.BigsuitSprite.spriteFrame = this.bigSuits[poker.suit];
        }
        this.SmallsuitSprite.spriteFrame = this.smallSuits[poker.suit];
        this.setStatus(poker.status);
    }
    private setStatus(status: EPokerStatus) {
        if (status == EPokerStatus.CLOSE) {
            this.pointLable.node.active = false;
            this.SmallsuitSprite.node.active = false;
            this.BigsuitSprite.node.active = false;
            this.bgSprite.spriteFrame = this.texBackBG;
        } else {
            this.pointLable.node.active = true;
            this.SmallsuitSprite.node.active = true;
            this.BigsuitSprite.node.active = true;
            this.bgSprite.spriteFrame = this.texFrontBG;
        }
    }
    public Refresh() {
        this.pointLable.node.active = true;
        this.SmallsuitSprite.node.active = true;
        this.BigsuitSprite.node.active = true;
        this.bgSprite.spriteFrame = this.texFrontBG;
    }

    public isOpen() :boolean{
        return this.poker.status==EPokerStatus.OPEN

    }
    public isPoint(point:number) :boolean{
        return this.poker.point==point

    }
}
